//
//  PageView.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/26.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

class PageView: UIView {

    private var style: PageViewStyle
    private var titles: [String]
    private var childViewControllers: [UIViewController]
    private var startIndex: Int
    
    private(set) lazy var titleView = PageTitleView(frame: .zero,
                                                             style: style,
                                                             titles: titles,
                                                             currentIndex: startIndex)
    
    private(set) lazy var contentView = PageContentView(frame: .zero,
                                                          style: style,
                                                          viewControllers: childViewControllers,
                                                          currentIndex: startIndex)
    
    init(frame: CGRect, style: PageViewStyle, titles: [String], childViewControllers: [UIViewController], startIndex: Int = 0) {
        self.style = style
        self.titles = titles
        self.childViewControllers = childViewControllers
        self.startIndex = startIndex
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        let titleFrame = CGRect(x: 0, y: 0, width: bounds.width, height: style.titleViewHeight)
        titleView.frame = titleFrame
        addSubview(titleView)
        
        let contentFrame = CGRect(x: 0, y: style.titleViewHeight, width: bounds.width, height: bounds.height - style.titleViewHeight)
        contentView.frame = contentFrame
        addSubview(contentView)
        
        titleView.delegate = contentView
        contentView.delegate = titleView
    }
    
    func moveToViewController(at index: Int) {
        titleView.selectedTitle(at: index)
    }
}
